//
//  PublicTabButtonPageViewController.m
//  PageControllerTest
//
//  Created by Seven-Augus on 2017/3/29.
//  Copyright © 2017年 Seven-Augus. All rights reserved.
//

#import "PublicTabButtonPageViewController.h"
#import "PublicTabPageTitleCollectionViewCell.h"

#define RGB_COLORS(r, g, b) [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1.0]
#define COLORSHEX(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface PublicTabButtonPageViewController ()

@property (nonatomic, assign) CGFloat selectFontScale;

@end


@implementation PublicTabButtonPageViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    
        _normalTextColor = [UIColor blackColor];
        _selectedTextColor =Color175030; // 选中时字体色彩
        
        _pagerBarColor = [UIColor whiteColor]; // 状态栏色彩
        _collectionViewBarColor = [UIColor whiteColor]; // 背景色
        
        _progressColor = _selectedTextColor; // 滚动条色
        _progressRadius = self.progressHeight / 2;
        
        [self registerCellClass:[PublicTabPageTitleCollectionViewCell class] isContainXib:NO];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.delegate = self;
    if (!self.dataSource) {
        self.dataSource = self;
    }
    _selectFontScale = self.normalTextFont.pointSize / self.selectedTextFont.pointSize;
    
    [self configureSubViews];
}

- (void)configureSubViews
{
    // progress
    self.progressView.backgroundColor = _progressColor;
    self.progressView.layer.cornerRadius = _progressRadius;
    self.progressView.layer.masksToBounds = YES;
    
    // tabBar
    self.pagerBarView.backgroundColor = _pagerBarColor;
    self.collectionViewBar.backgroundColor = _collectionViewBarColor;
}

- (void)setBarStyle:(PublicPageBarStyle)barStyle
{
    [super setBarStyle:barStyle];
    
    if (barStyle == PublicPageBarStyleCoverView) {
        self.progressColor = _selectedTextColor;
    } else {
        self.progressColor = _selectedTextColor;
    }
    self.progressRadius = self.progressHeight / 2;
}


#pragma mark - private
- (void)transitionFromCell:(UICollectionViewCell<PublicTabPageTitleProtocol> *)fromCell toCell:(UICollectionViewCell<PublicTabPageTitleProtocol> *)toCell
{
    if (fromCell) {
        fromCell.titleLabel.textColor = self.normalTextColor;
        fromCell.transform = CGAffineTransformMakeScale(self.selectFontScale, self.selectFontScale);
//        NSLog(@"fromCell.titleLabel.text == %@", fromCell.titleLabel.text);
    }
    
    if (toCell) {
        toCell.titleLabel.textColor = self.selectedTextColor;
        toCell.transform = CGAffineTransformIdentity;
//        NSLog(@"toCell.titleLabel.text == %@", toCell.titleLabel.text);
    }
}

- (void)transitionFromCell:(UICollectionViewCell<PublicTabPageTitleProtocol> *)fromCell toCell:(UICollectionViewCell<PublicTabPageTitleProtocol> *)toCell progress:(CGFloat)progress
{
    CGFloat currentTransform = (1.0 - self.selectFontScale) *progress;
    fromCell.transform = CGAffineTransformMakeScale(1.0 - currentTransform, 1.0 - currentTransform);
    toCell.transform = CGAffineTransformMakeScale(self.selectFontScale+currentTransform, self.selectFontScale+currentTransform);
    
    CGFloat narR, narG, narB, narA;
    [self.normalTextColor getRed:&narR green:&narG blue:&narB alpha:&narA];
    
    CGFloat selR, selG, selB, selA;
    [self.selectedTextColor getRed:&selR green:&selG blue:&selB alpha:&selA];
    
    CGFloat detalR = narR - selR, detalG = narG - selG, detalB = narB - selB, detalA = narA - selA;
    
    fromCell.titleLabel.textColor = [UIColor colorWithRed:selR + detalR *progress green:selG + detalG *progress blue:selB + detalB *progress alpha:selA + detalA *progress];
    toCell.titleLabel.textColor = [UIColor colorWithRed:narR - detalR*progress green:narG - detalG *progress blue:narB - detalB *progress alpha:narA - detalA *progress];
}


#pragma mark - PublicPageControllerDataSource
- (NSInteger)numberOfControllersInPagerController
{
    NSAssert(NO, @"you must impletement method numberOfControllersInPagerController");
    return 0;
}

- (UIViewController *)pagerController:(PublicPageViewController *)pagerController controllerForIndex:(NSInteger)index
{
    NSAssert(NO, @"you must impletement method pagerController:controllerForIndex:");
    return nil;
}


#pragma mark - PublicTabPageViewControllerDelegate
- (void)pagerController:(PublicTabPageViewController *)pagerController configreCell:(PublicTabPageTitleCollectionViewCell *)cell forItemTitle:(NSString *)title atIndexPath:(NSIndexPath *)indexPath
{
    PublicTabPageTitleCollectionViewCell *titleCell = (PublicTabPageTitleCollectionViewCell *)cell;
    titleCell.titleLabel.text = title;
    titleCell.titleLabel.font = self.selectedTextFont;
}

- (void)pagerController:(PublicTabPageViewController *)pagerController transitionFromeCell:(UICollectionViewCell<PublicTabPageTitleProtocol> *)fromCell toCell:(UICollectionViewCell<PublicTabPageTitleProtocol> *)toCell animated:(BOOL)animated
{
//    NSLog(@"fromCell.titleLabel.text == %@, toCell.titleLabel.text == %@", fromCell.titleLabel.text, toCell.titleLabel.text);
    if (animated) {
        [UIView animateWithDuration:self.animateDuration animations:^{
            [self transitionFromCell:(PublicTabPageTitleCollectionViewCell *)fromCell toCell:(PublicTabPageTitleCollectionViewCell *)toCell];
        }];
    } else {
        [self transitionFromCell:fromCell toCell:toCell];
    }
}

- (void)pagerController:(PublicTabPageViewController *)pagerController transitionFromeCell:(UICollectionViewCell<PublicTabPageTitleProtocol> *)fromCell toCell:(UICollectionViewCell<PublicTabPageTitleProtocol> *)toCell progress:(CGFloat)progress
{
    [self transitionFromCell:fromCell toCell:toCell progress:progress];
}


@end
